/*
 * @Author: @yzcheng
 * @Date: 2021-04-01 09:14:34
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description:
 * @LastEditTime: 2021-04-15 20:01:34
 */
import { getRoutes } from '@/api/global'
import { dyncRoutes, USE_REMOTE_ROUTE_TABLE } from '@/utils/routes'
import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from 'vue-router'
import { isMicroApp } from '@/utils/micro'
import navData from '@/i18n/nav.json'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageName = require('../../package.json').name

// 远程路由表数据
// export const remoteRoutes = getRoutes({ method: 'get' }).then((rst) => {
//   if (rst?.data?.code === 200) {
//     const data = rst.data.data.find((item: any): boolean => item.path === 'base-data/basedata')
//     if (data == undefined) {
//       return []
//     }
//     return data.children || []
//   } else {
//     return []
//   }
// })

// 返回router
// export const router = USE_REMOTE_ROUTE_TABLE
//   ? remoteRoutes.then((data) => {
//     return useRouter(dyncRoutes(data || []))
//   })
//   : new Promise(function (resolve) {
//     resolve(useRouter(dyncRoutes(navData['zh-cn'])))
//   })

// function useRouter(dyncRoutes: Array<any>) {
//   return createRouter({
//     history: createWebHistory(isMicroApp() ? '/' + packageName : '/'),
//     routes: [...BaseRoutes, ...dyncRoutes],
//   })
// }

// export const demo = '1'

export const router = new Promise(function (resolve) {
  resolve(useRouter(dyncRoutes(navData['zh-cn'])))
})

function useRouter(dyncRoutes: Array<any>) {
  return createRouter({
    // history: createWebHistory(isMicroApp() ? '/' + packageName : '/'),
    history: createWebHashHistory('/'),
    routes: [...dyncRoutes],
  })
}

export const demo = '1'
